/**
 * 渠道市场管理初始化
 */
var ChannelInfo = {
    id: "ChannelInfoTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
ChannelInfo.initColumn = function () {
    return [
        {field: 'selectItem', radio: false},
        // {title: '', field: 'id', visible: true, align: 'center', valign: 'middle'},
        {title: '渠道编码', field: 'code', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '渠道名称', field: 'name', visible: false, align: 'center', valign: 'middle', sortable: true},
        {title: '渠道名称', field: 'nameFormat', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '详情链接', field: 'detailUrl', visible: false, align: 'center', valign: 'middle', sortable: true},
        {title: '级别', field: 'level', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '搜索链接', field: 'searchUrl', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '状态', field: 'status', visible: false, align: 'center', valign: 'middle', sortable: true},
        {title: '状态', field: 'statusFormat', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '下载个数', field: 'downloadCount', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '描述', field: 'description', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '创建日期', field: 'createTime', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '更新日期', field: 'updateTime', visible: true, align: 'center', valign: 'middle', sortable: true}
    ];
};

/**
 * 检查是否选中
 */
ChannelInfo.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        ChannelInfo.seItem = selected[0];
        return true;
    }
};
/**
 * 检查是否选中
 */
ChannelInfo.multiCheck = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        ChannelInfo.seItems = selected;
        return true;
    }
};
/**
 * 点击添加渠道市场
 */
ChannelInfo.openAddChannelInfo = function () {
    var index = layer.open({
        type: 2,
        title: '添加渠道市场',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/channelInfo/channelInfo_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看渠道市场详情
 */
ChannelInfo.openChannelInfoDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '渠道市场详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/channelInfo/channelInfo_update/' + ChannelInfo.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除渠道市场
 */
ChannelInfo.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/channelInfo/delete", function (data) {
            Feng.success("删除成功!");
            ChannelInfo.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("channelInfoId", this.seItem.id);
        ajax.start();
    }
};
/**
 * 启用搜索关键字
 */
ChannelInfo.enable = function () {
    if (this.multiCheck()) {
        var ajax = new $ax(Feng.ctxPath + "/channelInfo/enable", function (data) {
            Feng.success("启用成功!");
            ChannelInfo.table.refresh();
        }, function (data) {
            Feng.error("启用失败!" + data.responseJSON.message + "!");
        });
        var ids = [];
        for (var i = 0; i < this.seItems.length; i++) {
            ids.push(this.seItems[i].id)
        }
        ajax.set("channelInfoIds", ids);
        ajax.start();
    }
};
/**
 * 停用搜索关键字
 */
ChannelInfo.disable = function () {
    if (this.multiCheck()) {
        var ajax = new $ax(Feng.ctxPath + "/channelInfo/disable", function (data) {
            Feng.success("停用成功!");
            ChannelInfo.table.refresh();
        }, function (data) {
            Feng.error("停用失败!" + data.responseJSON.message + "!");
        });
        var ids = [];
        for (var i = 0; i < this.seItems.length; i++) {
            ids.push(this.seItems[i].id)
        }
        ajax.set("channelInfoIds", ids);
        ajax.start();
    }
};
/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
ChannelInfo.formParams = function () {
    var queryData = {};

    if ($("#status").val() != "all") {
        queryData['status'] = $("#status").val();
    } else {
        queryData['status'] = null;
    }
    if ($("#level").val() != "all") {
        queryData['level'] = $("#level").val();
    } else {
        queryData['level'] = null;
    }
    queryData['condition'] = $("#condition").val();

    return queryData;
}

/**
 * 查询日志列表
 */
ChannelInfo.search = function () {
    ChannelInfo.table.refresh({query: ChannelInfo.formParams()});
};
/**
 * 导出csv文件
 */
ChannelInfo.exportCSV = function () {
    var ajax = new $ax(Feng.ctxPath + "/channelInfo/list", function (data) {
        var params = ChannelInfo.formParams();
        var fileName = '';
        if (params['condition'] != '') {
            fileName = '-' + params['condition'];
        }
        var titles = [];
        var keys = [];
        for (var i = 0; i < ChannelInfo.table.getVisibleColumns().length; i++) {
            var header = ChannelInfo.table.getVisibleColumns()[i];
            if (header['title'] != undefined && header['title'] != '') {
                titles.push(header['title']);
                headerField = header['field'];
                if (headerField.indexOf('Format') > -1) {
                    keys.push(headerField.substring(0, headerField.length-'Format'.length));
                } else {
                    keys.push(headerField);
                }
            }
        }
        JSonToCSV.setDataConver({
            data: data.rows,
            fileName: '渠道监控-渠道市场列表' + fileName,
            showLabel: true,
            columns: {
                title: titles, key: keys, formatter: function (key, value) {
                    if (value == undefined) {
                        return '';
                    } else {
                        return value.toString().replace(/,/g, ' ');
                    }
                }
            }
        });
        Feng.success("导出CSV成功!");
    }, function (data) {
        Feng.error("导出失败!" + data.responseJSON.message + "!");
    });
    ajax.setData(ChannelInfo.formParams());
    ajax.set('limit', 100000);
    ajax.set('offset', 0);
    ajax.start();
};
$(function () {
    var defaultColunms = ChannelInfo.initColumn();
    var table = new BSTable(ChannelInfo.id, "/channelInfo/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(ChannelInfo.formParams());
    ChannelInfo.table = table.init();
    Feng.initMultiSelect('level', 'status');
});
